package org.jeecg.modules.monitoring.j_monitoring.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xkcoding.http.util.StringUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.sign.util.NoSslHttpUtils;
import org.jeecg.config.vo.HttpResult;
import org.jeecg.modules.monitoring.j_monitoring.constantConfig.MonitorStatusEnum;
import org.jeecg.modules.monitoring.j_monitoring.entity.GatewaySystemFdn;
import org.jeecg.modules.monitoring.j_monitoring.entity.GatewayWarning;
import org.jeecg.modules.monitoring.j_monitoring.entity.GatewayWarningCount;
import org.jeecg.modules.monitoring.j_monitoring.entity.fdnEntity.Devices;
import org.jeecg.modules.monitoring.j_monitoring.mapper.*;
import org.jeecg.modules.monitoring.j_monitoring.service.IGatewayWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: gateway_warning
 * @Author: xiaoyq
 * @Date:   2023-05-09
 * @Version: V1.0
 */
@Service
public class GatewayWarningServiceImpl extends ServiceImpl<GatewayWarningMapper, GatewayWarning> implements IGatewayWarningService {


    @Autowired
    private Environment env;
    @Autowired
    private RedisUtil redisUtil;

    private String warningList = "/ng/alarm/getAlarms";

    private GatewaySystemFdnMapper fdnMapper;

    @Resource
    private MGlNetworkResourceMapper networkResourceMapper;
    @Resource
    private MGlComputingResourceMapper computingResourceMapper;
    @Resource
    private MGlStorageResourcesMapper storageResourcesMapper;
    @Resource
    private MGlVirtualizationClusterMapper virtualizationClusterMapper;




    public void syncGatewayWarning(){
        String url = env.getProperty("gatewaySystem.url");

        String header = env.getProperty("gatewaySystem.header");

        try {
            HttpResult res = NoSslHttpUtils.noSslGet(url+warningList,header);

            List<GatewayWarning> list = JSONArray.parseArray(res.getBody(), GatewayWarning.class);

            QueryWrapper<GatewayWarning> queryWrapper = new QueryWrapper<GatewayWarning>();
            List<GatewayWarning> oldList = baseMapper.selectList(queryWrapper);

            List<String> deleteIds = this.getDeleteIds(list,oldList);

            List<String> ipList = new ArrayList<>();
            /**
             * 删除不同项
             */
            if (deleteIds.size()>=1){
                baseMapper.deleteBatchIds(deleteIds);
            }

            for (GatewayWarning warning : list){

                if (this.canInsert(oldList,warning.getId())){
                    baseMapper.insert(warning);
                }else{
                    baseMapper.updateById(warning);
                }
                ipList.add(warning.getIp());
            }

            //根据ip更新监控设备告警状态
            ipList = ipList.stream().distinct().collect(Collectors.toList());//去重
            //更新 网络设备 计算资源 存储设备 虚拟集群
            networkResourceMapper.updateMonitorStatusByIpList(MonitorStatusEnum.warning.getKey(), ipList);
            computingResourceMapper.updateMonitorStatusByIpList(MonitorStatusEnum.warning.getKey(), ipList);
            virtualizationClusterMapper.updateMonitorStatusByIpList(MonitorStatusEnum.warning.getKey(), ipList);
            storageResourcesMapper.updateMonitorStatusByIpList(MonitorStatusEnum.warning.getKey(), ipList);


        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e);
        }

    }

    private Boolean canInsert(List<GatewayWarning> warningList,String id){

        for (GatewayWarning warning : warningList){

            if (warning.getId().equals(id)){
                return false;
            }

        }
        return true;
    }

    private List<String> getDeleteIds(List<GatewayWarning> newList,List<GatewayWarning> oldList){

        List<String> newString = new ArrayList<>();
        List<String> oldString = new ArrayList<>();
        for (GatewayWarning newWarning : newList){
            newString.add(newWarning.getId());
        }
        for (GatewayWarning oldWarning : oldList){
            oldString.add(oldWarning.getId());
        }
        oldString.removeAll(newString);
        return oldString;
    }

    public GatewayWarningCount getWarningCount(String fdn){
        return baseMapper.getWarningCount(fdn);
    }

    public Map<String,GatewayWarning> getWarningGroupByIp(List<String> ipList){
        List<GatewayWarning> warningList =  baseMapper.getAllGroupIp();
        if (warningList.size()==0){
            return new HashMap<>();
        }

        Map<String,GatewayWarning> result = new HashMap<>();

        for (GatewayWarning warning : warningList){
            result.put(warning.getIp(),warning);
        }
        return result;
    }




    public Map<String,List<GatewayWarning>> getWarningListGroupByIp(List<String> ipList){
        List<GatewayWarning> warningList =  baseMapper.getAllGroupIp();
        if (warningList.size()==0){
            return new HashMap<>();
        }

        Map<String,List<GatewayWarning>> result = new HashMap<>();

        for (GatewayWarning warning : warningList){

            if (result.get(warning.getIp())==null){
                List<GatewayWarning> list = new ArrayList<>();
                list.add(warning);
                result.put(warning.getIp(),list);
            }else{
                List<GatewayWarning> list = result.get(warning.getIp());
                list.add(warning);
                result.put(warning.getIp(),list);
            }
        }
        return result;
    }

    @Override
    public void removeAll() {
        this.baseMapper.removeAll();
    }

    public List<GatewayWarning> getWarningByFdnList(List<String> fdnList){
        QueryWrapper<GatewayWarning> queryWrapper = new QueryWrapper<GatewayWarning>();
        for (String fdn : fdnList){
            queryWrapper.or().like("fdn", fdn);
        }
//        queryWrapper.or()
//        queryWrapper.in("fdn",fdnList);
        List<GatewayWarning> gatewayFdnList = baseMapper.selectList(queryWrapper);
        return gatewayFdnList;
    }

}
